{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# NumPy IO - save\n",
    "#\n",
    "# 常用的 IO 函数有：\n",
    "# load() 和 save() 函数是读写文件数组数据的两个主要函数，默认情况下，数组是以未压缩的原始二进制格式保存在扩展名为 .npy 的文件中。\n",
    "# savez() 函数用于将多个数组写入文件，默认情况下，数组是以未压缩的原始二进制格式保存在扩展名为 .npz 的文件中。\n",
    "# loadtxt() 和 savetxt() 函数处理正常的文本文件(.txt 等)\n",
    "#\n",
    "import numpy as np\n",
    "\n",
    "def save_npy():\n",
    "    '''\n",
    "    numpy.save(file, arr, allow_pickle=True, fix_imports=True)\n",
    "    参数说明：\n",
    "    file:           要保存的文件, 扩展名为 .npy, 如果文件路径末尾没有扩展名 .npy, 该扩展名会被自动加上。\n",
    "    arr:            要保存的数组\n",
    "    allow_pickle:   可选，布尔值，允许使用 Python pickles 保存对象数组, Python 中的 pickle 用于在保存到磁盘文件或从磁盘文件读取之前，对对象进行序列化和反序列化。\n",
    "    fix_imports:    可选，为了方便 Pyhton2 中读取 Python3 保存的数据。\n",
    "    '''\n",
    "    a = np.array([1, 2, 3, 4, 5])\n",
    "    # 保存到 outfile.npy 文件上\n",
    "    # 如果文件路径末尾没有扩展名 .npy，该扩展名会被自动加上\n",
    "    np.save('./outfile.npy', a)\n",
    "\n",
    "    # 读取数据\n",
    "    b = np.load('./outfile.npy')\n",
    "    print(b)\n",
    "    return\n",
    "\n",
    "\n",
    "save_npy()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# NumPy IO - savez\n",
    "# \n",
    "import numpy as np\n",
    "\n",
    "def save_npz():\n",
    "    '''\n",
    "    numpy.savez(file, *args, **kwds)\n",
    "    函数将多个数组保存到以 npz 为扩展名的文件中。\n",
    "    参数说明：\n",
    "    file: 要保存的文件，扩展名为 .npz. 如果文件路径末尾没有扩展名 .npz, 该扩展名会被自动加上。\n",
    "    args: 要保存的数组，可以使用关键字参数为数组起一个名字，非关键字参数传递的数组会自动起名为 arr_0, arr_1, …　\n",
    "    kwds: 要保存的数组使用关键字名称\n",
    "    '''    \n",
    "    a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "    b = np.arange(0, 1.0, 0.1)\n",
    "    c = np.sin(b)\n",
    "\n",
    "    # 保存1\n",
    "    # np.savez(\"./runoob.npz\", a, b, c)\n",
    "    # r = np.load(\"./runoob.npz\")\n",
    "    # print(r.files)      # 查看各个数组名称\n",
    "    # print(r[\"arr_0\"])   # 数组 a\n",
    "    # print(r[\"arr_1\"])   # 数组 b\n",
    "    # print(r[\"arr_2\"])   # 数组 c\n",
    "\n",
    "    # 保存2 - 数组使用关键字名称\n",
    "    np.savez(\"./runoob.npz\", a, b, sin_array=c)\n",
    "    r = np.load(\"./runoob.npz\")\n",
    "    print(r.files)          # 查看各个数组名称\n",
    "    print(r[\"arr_0\"])       # 数组 a\n",
    "    print(r[\"arr_1\"])       # 数组 b\n",
    "    print(r[\"sin_array\"])   # 数组 c\n",
    "    return\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# NumPy IO - savetxt\n",
    "#\n",
    "import numpy as np\n",
    "\n",
    "def save_txt():\n",
    "    '''\n",
    "    savetxt()\n",
    "    np.savetxt(FILENAME, a, fmt=\"%d\", delimiter=\",\")\n",
    "    np.loadtxt(FILENAME, dtype=int, delimiter=' ')\n",
    "    以简单的文本文件格式存储数据，对应的使用 loadtxt() 函数来获取数据\n",
    "    '''\n",
    "    # a = np.array([1, 2, 3, 4, 5])\n",
    "    # np.savetxt('out.txt', a)\n",
    "    # b = np.loadtxt('out.txt')\n",
    "    # print(b)\n",
    "\n",
    "    a = np.arange(0, 10, 0.5).reshape(4, -1)\n",
    "    print(a)\n",
    "    np.savetxt(\"out.txt\", a, fmt=\"%d\", delimiter=\",\")  # 改为保存为整数，以逗号分隔\n",
    "    b = np.loadtxt(\"out.txt\", delimiter=\",\")  # load 时也要指定为逗号分隔\n",
    "    print(b)\n",
    "\n",
    "    return\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.7 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "60d0b561bcf179bd0cd76361af4313e00281c3c5daca1903769e6cc3dd82b37c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
